<table id="teamoverview" class="table table-striped table-condensed">
    <!-- Row Filters -->
    <?php /*
      <!--
      <tr class="row-filters">
      <td>Filter</td>
      <td><input type="text" class="input-medium" placeholder="Namen eingeben"></td>
      <td>
      <select class="input-medium">
      <option>Kein Filter</option>
      <option>Reality Training</option>
      <option>Rush Up International</option>
      </select>
      </td>
      <td>
      <select class="input-medium">
      <option>Kein Filter</option>
      <option>SOA Workshop</option>
      </select>
      </td>
      <td>
      <select class="input-medium">
      <option>Kein Filter</option>
      <option>Rhetorik</option>
      </select>
      </td>
      <td>
      <a href="" class="btn btn-small">Filter anwenden</a>
      </td>
      </tr>
      -->
     */ ?>
    <!-- Row Headlines -->
    <tr>
        <th>#</th>
        <th>Mitarbeiter <?php /* <a href=""><img src="<?php echo base_url(); ?>img/carret_down.png"></a><a href=""><img src="<?php echo base_url(); ?>img/carret_up.png"></a> */ ?></th>
        <th>1. Maßnahme <?php /* <a href=""><img src="/img/carret_down.png"></a><a href=""><img src="img/carret_up.png"></a> */ ?></th>
        <th>2. Maßnahme <?php /* <a href=""><img src="/img/carret_down.png"></a><a href=""><img src="img/carret_up.png"></a> */ ?></th>
        <th>3. Maßnahme <?php /* <a href=""><img src="/img/carret_down.png"></a><a href=""><img src="img/carret_up.png"></a> */ ?></th>
        <th>Gesamtkosten <?php /* <a href=""><img src="<img/carret_down.png"></a><a href=""><img src="img/carret_up.png"></a> */ ?><br >
            <span>Fix/InHouse</span>
        </th>
    </tr>


    <?php if (isset($team)): ?>
        <?php
        $j = 1;
        $total_cost = 0;
        $amount = 0;
        $row_cost = 0;
        ?>
        <?php foreach ($team->employee() as $employee): ?>
            <?php $contactdata = $employee->contactdata(); ?>
            <tr>
                <td><?php echo $j++; ?></td>
                <td>
                    <?php
                    echo anchor('team/show_member/' . $employee->id, $contactdata->surname . ',' . $contactdata->forename);
                    $new_year = date_format(date_create($employee->new_since), "Y");
                    if ($new_year === date('Y')) {
                        //echo '<span rel="tooltip" title="Achtung: Neuer Mitarbeiter - Dieser hat ein höheres Budget." class="badge-newemployer label label-warning">Neuer MA</span>';
                    }
                    echo '<br>';
                    echo '<span>Pers.Nr.:' . $employee->id . '</span>';
                    ?>
                    <?php
                    $demand = $employee->stopedemand();

                    for ($i = 0; $i < 3; ++$i) {
                        echo '<td>';
                        if (!isset($demand[$i])) {
                            echo '</td>';
                            continue;
                        }

                        if ($demand[$i]->inactive !== '0' && $demand[$i]->course_id !== NULL) {
                            $training = FALSE;

                            if (!is_null($demand[$i]->course_id)) {
                                $course = $demand[$i]->course();
                            }

                            $active_training = NULL;
                            /**
                             * @TODO: Choose correct traininggroup and training to display the correct cost amount
                             * 			Employee->Confirmation->Training
                             */
                            $traininggroup_arr = $course->traininggroup();
                            foreach ($traininggroup_arr as $traininggroup) {
                                foreach ($traininggroup->training() as $training) {
                                    if ($training->trainingstatus()->name != 'Storniert' && $training->canceled != '1') {
                                        $active_training = $training;
                                        break;
                                    }
                                }
                                if ($active_training)
                                    break;
                            }
                            if ($active_training) {
                                $cost_arr = $active_training->traininggroup()->cost();
                                foreach ($cost_arr as $cost) {
                                    $amount += $cost->amount;
                                }
                            } else if ($course && $traininggroup_arr == NULL) {
                                $cost_arr = $course->cost();
                                
                                /**
                                 * Fixed by FB 06.02.13
                                 */
                                
                                if(is_object($cost_arr)) {
                                    $amount += $cost_arr->amount;
                                } else {
                                    foreach ($cost_arr as $cost) {
                                        $amount += $cost->amount;
                                    }
                                }
                            }
                        }


                        $row_cost += $amount;
                        $amount = 0;

                        echo $this->load->view('training/block', array('demand' => $demand[$i]));

                        echo '</td>';
                    }
                    ?>


                <td>
                    <?php
                    echo number_format($row_cost, 2, ',', '.') . '€';
                    $total_cost += $row_cost;
                    $row_cost = 0;
                    ?>
                </td>
            </tr>

        <?php endforeach; ?>
    <?php endif; ?>

    <tr>
        <td colspan="5" style="text-align:right;">Gesamtkosten(Team):</td><td>
            <?php
            if (!isset($total_cost)) {
                $total_cost = 0;
            }
            echo number_format($total_cost, 2, ',', '.') . '€';
            ?>
        </td>
    </tr>
    <tr><td colspan="5" style="text-align:right;">Teambudget Gesamt:</td><td><?php echo ($team->budget) != NULL ? number_format($team->budget, 2, ',', '.') . '€' : 'N/A'; ?></td></tr>
    <tr><td colspan="5" style="text-align:right;">Teambudget verbleibend:</td>
        <?php
        $teambudget = floatval($team->budget);

        if (isset($teambudget)) {
            if (0 > ($teambudget - $total_cost)) {
                echo '<td class="red">';
            } else {
                echo '<td>';
            }

            echo number_format($teambudget - $total_cost, 2, ',', '.') . '€</td>';
        }
        ?>

    </tr>
</table>

<div class="tmt_box" style="position: fixed; bottom: 72px; left:0; padding:5px; margin:15px;">
    <table id="legend-team">
        <tr><th colspan="2">Legende:</th></tr>
        <tr><td><div class="dot dot-blue"></div>Maßnahme in Planung</td></tr>
        <tr><td><div class="dot dot-orange"></div>Maßnahme gebucht</td></tr>
        <tr><td><div class="dot dot-green"></div>Maßnahme fakturiert</td></tr>
        <tr><td><div class="legend-box gray"></div>Schulung deaktiviert</td></tr>
        <tr><td><div class="legend-box red"></div>Schulung storniert</td></tr>

    </table>
</div>
<!-- Content End -->